package com.jspgou.cms.action.front;

import static com.jspgou.cms.Constants.STORE_SYS;
import static com.jspgou.common.page.SimplePage.cpn;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import com.jspgou.cms.entity.Coupon;
import com.jspgou.cms.entity.MemberCoupon;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.CouponMng;
import com.jspgou.cms.manager.MemberCouponMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.cms.web.threadvariable.MemberThread;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.CookieUtils;
import com.jspgou.common.web.RequestUtils;
import com.jspgou.common.web.ResponseUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.User;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.UserMng;
import com.jspgou.core.web.front.URLHelper;

@Controller
public class CouponAct {
	/**
	 * 店铺优惠券管理列表页
	 */
	private static final String TPL_COUPON_LIST = "tpl.couponList";
	/**
	 * 店铺优惠券管理添加页
	 */
	private static final String TPL_COUPON_ADD = "tpl.couponAdd";
	/**
	 * 领取优惠券页
	 */
	private static final String TPL_COUPON = "tpl.coupon";
	/**
	 * 店铺优惠券管理修改页
	 */
	private static final String TPL_COUPON_UPDATE = "tpl.couponUpdate";

	@RequestMapping(value = "/coupon/list*.jspx")
	/*@RequestMapping(value ={"/coupon/v_list*.do", "/coupon/v_list*.jspx"}) */
	public String coupon(String cl,Integer pageNo,HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		// 用户没有登录，跳转到登录页
		if (member == null) {
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store = member.getStore();
		if (store == null) {
			return "redirect:registerStore.jspx";
		}		
		model.addAttribute("store", store);
		model.addAttribute("cl", cl);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS,MessageResolver.getMessage(request, TPL_COUPON_LIST), request);
	}

	/**
	 * 
	 * @param bean
	 * @param request
	 * @param response
	 *            店铺优惠券添加
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/coupon/couponAdd.jspx")
	public String add(Coupon bean, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		// 用户没有登录，跳转到登录页
		if (member == null) {
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store = member.getStore();
		if (store == null) {
			return "redirect:registerStore.jspx";
		}
         model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS,MessageResolver.getMessage(request, TPL_COUPON_ADD), request);
		
	}

	/**
	 * 店铺优惠券修改
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/coupon/couponUpdate.jspx")
	public String update(Long id, Coupon bean, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		// 用户没有登录，跳转到登录页
		if (member == null) {
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store = member.getStore();
		if (store == null) {
			return "redirect:registerStore.jspx";
		}
		Coupon coupon = couponMng.findById(id);
		model.addAttribute("coupon", coupon);
		couponMng.update(bean, null);
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS,MessageResolver.getMessage(request, TPL_COUPON_UPDATE), request);
		
	}

	/**
	 * 店铺优惠券删除
	 * 
	 * @param id
	 * @param pageNo
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/coupon/couponDelete.jspx")
	public String delete(Long id, Integer pageNo, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		// 用户没有登录，跳转到登录页
		if (member == null) {
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store = member.getStore();
		if (store == null) {
			return "redirect:registerStore.jspx";
		}

		Coupon coupon = couponMng.findById(id);
		couponMng.deleteById(id, null);
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS,
				MessageResolver.getMessage(request, TPL_COUPON_LIST), request);
	}

	/**
	 * 优惠券保存
	 * 
	 * @param bean
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/coupon/couponSave.jspx")
	public String save(Coupon bean, Integer pageNo,HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		String upRoot = web.getUploadUrl();
		model.addAttribute("uploadRoot", upRoot);
		Store store = member.getStore();
		bean.setStore(store);
		couponMng.save(bean, web.getId(), null);
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		 return web.getTplSys(STORE_SYS,
		 MessageResolver.getMessage(request,TPL_COUPON_ADD), request);
	}

	/**
	 * 
	 * @param id
	 * @param request
	 * @param response
	 *            领取优惠券
	 * @param model
	 * @throws JSONException
	 */
	@RequestMapping(value = "/getcoupon.jspx")
	public void fetchGift(Long id, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = MemberThread.get();
		Date newdate = new Date();
		JSONObject json = new JSONObject();
		if (member == null) {
			json.put("status", 0);
		} else if (id == null || couponMng.findById(id) == null) {
			json.put("status", 2);
			json.put("error", "参数有误");
		} else if (memberCouponMng.findByCoupon(member.getId(), id) != null) {
			json.put("status", 2);
			json.put("error", "您已领取过该优惠券了");
		} else if (!newdate.before(couponMng.findById(id).getCouponEndTime())) {
			json.put("status", 2);
			json.put("error", "该优惠券已过期");
		} else {
			Coupon coupon = couponMng.findById(id);
			MemberCoupon memberCoupon = new MemberCoupon();
			memberCoupon.setCoupon(coupon);
			memberCoupon.setMember(member);
			memberCoupon.setIsuse(false);
			memberCouponMng.save(memberCoupon);
			json.put("status", 1);
		}
		ShopFrontHelper.setCommonData(request, model, web);
		ResponseUtils.renderJson(response, json.toString());
	}

	/**
	 * 店铺优惠券页
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/coupon.jspx")
	public String coupon1(Integer pageNo,HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store = member.getStore();
		int pageNo1 = URLHelper.getPageNo(request);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.setDynamicPageData(request, model, web,
				RequestUtils.getLocation(request), "coupon", ".jspx", pageNo1);
		if(store==null){
			return "redirect:registerStore.jspx";
		}else{			
			if(store.getId()==1){
				Pagination coupons=couponMng.getPage(null,cpn(pageNo), 6);
				model.addAttribute("pagination", coupons);
			}else{
				Pagination coupons=couponMng.getPage(store.getId(),cpn(pageNo), 6);
				model.addAttribute("pagination", coupons);
			}	
		}
		return web.getTplSys(STORE_SYS,
				MessageResolver.getMessage(request, "tpl.coupon"), request);
	}

	@Autowired
	private UserMng userMng;
	@Autowired
	private CouponMng couponMng;
	@Autowired
	private MemberCouponMng memberCouponMng;

}
